<?php

namespace Luka\NavigationBundle\Twig\Extension;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Templating\Helper\Helper;
use Symfony\Component\HttpFoundation\ParameterBag;

class PagenationExtension extends \Twig_Extension{
    public function getFunctions(){
        return array(
            "ln_query_string"  => new \Twig_Function_Method($this, "getQueryString")
        );
    }

    public function getQueryString(){
        $values = func_get_args();
        if(!empty($values)){
            $key = array_shift($values);
            if(!empty($key)){
                unset($values[$key]);
                $parameterBag = new ParameterBag($_REQUEST);
                $value = implode("-", $values);

                if($parameterBag->has($key)){
                    $parameterBag->set($key, $value);
                }
                else{
                    $parameterBag->add(array($key => $value));
                }

                $output = array();
                foreach($parameterBag->all() as $k => $v){
                    $output[] = $k.'='.$v;
                }

                return "?".implode("&", $output);
            }
        }
        return "";
    }

    public function getName() {
        return "luka_pagenation_extension";
    }
}
